|
1
|
|
|
console.log('Yii2 files model init.'); |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
var currentCroppingImageId; |
|
5
|
|
|
var currentRenamingFileId; |
|
6
|
|
|
var cropper; |
|
7
|
|
|
var removeFileOnCropCancel; |
|
8
|
|
|
var yii2CropperRoute; |
|
9
|
|
|
var yii2UploadRoute; |
|
10
|
|
|
var uploaderSettings = {}; |
|
11
|
|
|
|
|
12
|
|
|
$(document).on('change', '.yii2-files-upload-field', function () { |
|
13
|
|
|
|
|
14
|
|
|
obj = $(this); |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
var formData = new FormData(); |
|
17
|
|
|
formData.append('file', obj[0].files[0]); |
|
18
|
|
|
formData.append('modelClass', obj.data('modelclass')); |
|
19
|
|
|
formData.append('attribute', obj.data('attribute')); |
|
20
|
|
|
formData.append('mode', obj.data('mode')); |
|
21
|
|
|
formData.append('ratio', obj.data('ratio')); |
|
22
|
|
|
formData.append('count', 1); |
|
23
|
|
|
formData.append('_fileFormToken', yii2FileFormToken); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
$.ajax({ |
|
27
|
|
|
url: yii2UploadRoute, |
|
|
|
|
|
|
28
|
|
|
type: 'POST', |
|
29
|
|
|
data: formData, |
|
30
|
|
|
processData: false, // tell jQuery not to process the data |
|
31
|
|
|
contentType: false, // tell jQuery not to set contentType |
|
32
|
|
|
success: function (response) { |
|
33
|
|
|
id = '#files-widget-block_' + obj.data('block'); |
|
|
|
|
|
|
34
|
|
|
$(response).appendTo(id).find('div.floor12-files-widget-list'); |
|
35
|
|
|
} |
|
36
|
|
|
}); |
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
function clipboard(text) { |
|
41
|
|
|
//based on https://stackoverflow.com/a/12693636 |
|
42
|
|
|
document.oncopy = function (event) { |
|
43
|
|
|
event.clipboardData.setData("Text", text); |
|
44
|
|
|
event.preventDefault(); |
|
45
|
|
|
}; |
|
46
|
|
|
document.execCommand("Copy"); |
|
47
|
|
|
document.oncopy = undefined; |
|
48
|
|
|
f12notification.info(text, 1); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
function updateProgressCircle(val, btnGroup) { |
|
52
|
|
|
result = 169.646 * (1 - val / 100); |
|
|
|
|
|
|
53
|
|
|
btnGroup.querySelector('svg #progress-circle').setAttribute('stroke-dashoffset', result); |
|
54
|
|
|
btnGroup.querySelector('.floor12-file-percents').innerHTML = val + '%'; |
|
55
|
|
|
// setAttribute('stroke-dashoffset', result); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
var observer = new MutationObserver(function (mutations) { |
|
|
|
|
|
|
59
|
|
|
percent = mutations[0].target.style.width.replace('%', ''); |
|
|
|
|
|
|
60
|
|
|
btnGroup = mutations[0].target.parentElement.parentElement; |
|
|
|
|
|
|
61
|
|
|
updateProgressCircle(percent, btnGroup); |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
var lastUploader = null; |
|
65
|
|
|
|
|
66
|
|
|
function Yii2FilesUploaderSet(id, className, attribute, scenario) { |
|
67
|
|
|
|
|
68
|
|
|
var mode = 'multi'; |
|
69
|
|
|
var blockName = "#" + id; |
|
70
|
|
|
var block = $(blockName); |
|
71
|
|
|
var uploadButton = block.find('button.btn-upload')[0]; |
|
72
|
|
|
var filesList = block.find('.floor12-files-widget-list')[0]; |
|
73
|
|
|
var ratio = 0; |
|
74
|
|
|
|
|
75
|
|
|
var csrf = block.parents('form').find('input[name=' + yii2CsrfParam + ']').val(); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if (block.data('ratio')) |
|
78
|
|
|
ratio = block.data('ratio'); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
if (block.hasClass('floor12-files-widget-single-block')) { |
|
81
|
|
|
mode = 'single'; |
|
82
|
|
|
toggleSingleUploadButton(block); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
uploaderSettings[id] = { |
|
86
|
|
|
modelClass: className, |
|
87
|
|
|
attribute: attribute, |
|
88
|
|
|
scenario: scenario, |
|
89
|
|
|
mode: mode, |
|
90
|
|
|
ratio: ratio, |
|
91
|
|
|
count: block.find('.floor12-files-widget-list .floor12-file-object').length, |
|
92
|
|
|
_fileFormToken: yii2FileFormToken |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
uploaderSettings[id][yii2CsrfParam] = csrf |
|
96
|
|
|
console.log(uploaderSettings[id]); |
|
|
|
|
|
|
97
|
|
|
var uploader = new ss.SimpleUpload({ |
|
|
|
|
|
|
98
|
|
|
button: uploadButton, |
|
99
|
|
|
url: yii2UploadRoute, |
|
|
|
|
|
|
100
|
|
|
name: 'file', |
|
101
|
|
|
dropzone: block, |
|
102
|
|
|
dragClass: 'floor12-files-widget-block-drug-over', |
|
103
|
|
|
multiple: true, |
|
104
|
|
|
multipleSelect: true, |
|
105
|
|
|
data: uploaderSettings[id], |
|
106
|
|
|
onSubmit: function (filename, extension, uploadBtn, size) { |
|
|
|
|
|
|
107
|
|
|
uploaderSettings[id].count++; |
|
108
|
|
|
var svg = '\t<svg class="progress-circle" width="60" height="60" viewBox="0 0 60 60">\n' + |
|
109
|
|
|
'\t\t<circle cx="30" cy="30" r="27" fill="none" stroke="#ccc" stroke-width="5" />\n' + |
|
110
|
|
|
'\t\t<circle id="progress-circle" cx="30" cy="30" r="27" fill="none" stroke="#666" stroke-width="5" stroke-dasharray="169.646" stroke-dashoffset="169.646" />\n' + |
|
111
|
|
|
'\t</svg>'; |
|
112
|
|
|
|
|
113
|
|
|
var fileId = generateId(filename); |
|
114
|
|
|
var btnGroup = document.createElement('div'); |
|
115
|
|
|
var fileObject = document.createElement('div'); |
|
116
|
|
|
var bar = document.createElement('div'); |
|
117
|
|
|
var percents = document.createElement('div'); |
|
118
|
|
|
btnGroup.setAttribute('id', fileId); |
|
119
|
|
|
btnGroup.className = 'btn-group files-btn-group'; |
|
120
|
|
|
fileObject.className = 'floor12-file-object'; |
|
121
|
|
|
percents.className = 'floor12-file-percents'; |
|
122
|
|
|
this.setProgressBar(bar); |
|
123
|
|
|
|
|
124
|
|
|
fileObject.innerHTML = svg; |
|
125
|
|
|
|
|
126
|
|
|
observer.observe(bar, { |
|
127
|
|
|
attributes: true |
|
128
|
|
|
}); |
|
129
|
|
|
|
|
130
|
|
|
fileObject.appendChild(bar); |
|
131
|
|
|
fileObject.appendChild(percents); |
|
132
|
|
|
btnGroup.appendChild(fileObject); |
|
133
|
|
|
|
|
134
|
|
|
if (mode == 'single') { |
|
135
|
|
|
$(filesList).html(''); |
|
136
|
|
|
} |
|
137
|
|
|
$(filesList).append(btnGroup); |
|
138
|
|
|
}, |
|
139
|
|
|
onComplete: function (filename, response) { |
|
140
|
|
|
if (!response) { |
|
141
|
|
|
console.log(filename + 'upload failed'); |
|
|
|
|
|
|
142
|
|
|
uploaderSettings[id].count--; |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
f12notification.info(FileUploadedText, 1); |
|
|
|
|
|
|
146
|
|
|
idName = "#" + generateId(filename); |
|
|
|
|
|
|
147
|
|
|
$(idName).replaceWith($(response)); |
|
148
|
|
|
if (mode == 'single') |
|
|
|
|
|
|
149
|
|
|
toggleSingleUploadButton(block); |
|
|
|
|
|
|
150
|
|
|
}, |
|
151
|
|
|
onError: function (filename, errorType, status, statusText, response, uploadBtn, fileSize) { |
|
|
|
|
|
|
152
|
|
|
console.log(uploaderSettings[id]); |
|
|
|
|
|
|
153
|
|
|
uploaderSettings[id].count--; |
|
154
|
|
|
data = { |
|
|
|
|
|
|
155
|
|
|
responseText: response, |
|
156
|
|
|
status: status, |
|
157
|
|
|
statusText: statusText, |
|
158
|
|
|
}; |
|
159
|
|
|
processError(data); |
|
160
|
|
|
idName = "#" + generateId(filename); |
|
|
|
|
|
|
161
|
|
|
$(idName).remove(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// progressUrl: 'uploadProgress.php', // enables cross-browser progress support (more info below) |
|
165
|
|
|
// responseType: 'json', |
|
166
|
|
|
// allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], |
|
167
|
|
|
// maxSize: 1024, // kilobytes |
|
168
|
|
|
// hoverClass: 'ui-state-hover', |
|
169
|
|
|
// focusClass: 'ui-state-focus', |
|
170
|
|
|
// disabledClass: 'ui-state-disabled', |
|
171
|
|
|
}); |
|
172
|
|
|
lastUploader = uploader; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
function generateId(filename) { |
|
176
|
|
|
return 'id-' + filename.replace(/[^0-9a-zA-Z]/g, ""); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
function showUploadButton(event) { |
|
180
|
|
|
obj = $(event.target); |
|
|
|
|
|
|
181
|
|
|
obj.parents('div.floor12-files-widget-single-block').find('button').show(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
function toggleSingleUploadButton(block) { |
|
185
|
|
|
if (block.find('div.floor12-single-file-object').length > 0) |
|
186
|
|
|
block.find('button').hide(); |
|
|
|
|
|
|
187
|
|
|
else |
|
188
|
|
|
block.find('button').show(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
function sortableFiles() { |
|
192
|
|
|
$(".floor12-files-widget-list-multi").sortable({ |
|
193
|
|
|
opacity: 0.5, |
|
194
|
|
|
revert: 1, |
|
195
|
|
|
items: "div.files-btn-group", |
|
196
|
|
|
connectWith: ".floor12-files-widget-list-multi" |
|
197
|
|
|
}); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
function removeFile(id) { |
|
201
|
|
|
id = "#yii2-file-object-" + id; |
|
202
|
|
|
const blockId = $(id).parents('.files-widget-block').attr('id'); |
|
203
|
|
|
|
|
204
|
|
|
$(id).parents('div.files-btn-group').fadeOut(200, function () { |
|
205
|
|
|
$(this).remove(); |
|
206
|
|
|
f12notification.info(FileRemovedText, 1); |
|
|
|
|
|
|
207
|
|
|
}); |
|
208
|
|
|
|
|
209
|
|
|
uploaderSettings[blockId].count--; |
|
210
|
|
|
return false; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
function removeAllFiles(event) { |
|
214
|
|
|
console.log('removeAllFiles'); |
|
|
|
|
|
|
215
|
|
|
$(event.target).parents('div.floor12-files-widget-list').find('div.files-btn-group').fadeOut(200, function () { |
|
216
|
|
|
$(this).remove(); |
|
217
|
|
|
}); |
|
218
|
|
|
const blockId = $(event.target).parents('.floor12-files-widget-block').attr('id'); |
|
219
|
|
|
uploaderSettings[blockId].count = 0; |
|
220
|
|
|
f12notification.info(FilesRemovedText, 1); |
|
|
|
|
|
|
221
|
|
|
return false; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
function initCropperLayout() { |
|
225
|
|
|
if (yii2CropperRoute.length > 0) |
|
|
|
|
|
|
226
|
|
|
$.get(yii2CropperRoute, function (response) { |
|
|
|
|
|
|
227
|
|
|
$('body').append(response); |
|
228
|
|
|
|
|
229
|
|
|
$('#yii2-file-title-editor input').on('keyup', function (e) { |
|
230
|
|
|
if (e.keyCode == 13) { |
|
231
|
|
|
saveFileTitle() |
|
232
|
|
|
} |
|
233
|
|
|
}); |
|
234
|
|
|
}) |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
function initCropper(id, url, ratio, remove) { |
|
238
|
|
|
$('#cropperModal').modal({keyboard: false, backdrop: 'static'}); |
|
239
|
|
|
|
|
240
|
|
|
currentCroppingImageId = id; |
|
241
|
|
|
|
|
242
|
|
|
removeFileOnCropCancel = false; |
|
243
|
|
|
if (remove) |
|
244
|
|
|
removeFileOnCropCancel = true; |
|
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
currentCropImage = $('<img>').attr('src', url); |
|
|
|
|
|
|
247
|
|
|
$('#cropperArea').html(""); |
|
248
|
|
|
$('#cropperArea').append(currentCropImage); |
|
249
|
|
|
|
|
250
|
|
|
autoCrop = false; |
|
|
|
|
|
|
251
|
|
|
aspectRatio = NaN; |
|
|
|
|
|
|
252
|
|
|
$('#cropper-btn-cancel').show(); |
|
253
|
|
|
|
|
254
|
|
|
console.log(cropperHideCancel); |
|
|
|
|
|
|
255
|
|
|
if (cropperHideCancel == 'true') { |
|
256
|
|
|
$('#cropper-btn-cancel').hide(); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
if (ratio) { |
|
261
|
|
|
autoCrop = true; |
|
262
|
|
|
aspectRatio = ratio; |
|
263
|
|
|
$('.cropper-ratio-btn-group').hide(); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
setTimeout(function () { |
|
267
|
|
|
cropper = currentCropImage.cropper({ |
|
268
|
|
|
viewMode: 1, |
|
269
|
|
|
background: false, |
|
270
|
|
|
zoomable: false, |
|
271
|
|
|
autoCrop: autoCrop, |
|
272
|
|
|
aspectRatio: aspectRatio, |
|
273
|
|
|
}); |
|
274
|
|
|
}, 1000) |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
function stopCrop(id) { |
|
|
|
|
|
|
278
|
|
|
$('#cropperModal').modal('hide'); |
|
279
|
|
|
if (removeFileOnCropCancel) |
|
280
|
|
|
removeFile(currentCroppingImageId); |
|
|
|
|
|
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
function cropImage() { |
|
284
|
|
|
сropBoxData = cropper.cropper('getCropBoxData'); |
|
|
|
|
|
|
285
|
|
|
imageData = cropper.cropper('getImageData'); |
|
|
|
|
|
|
286
|
|
|
canvasData = cropper.cropper('getCanvasData'); |
|
|
|
|
|
|
287
|
|
|
ratio = imageData.height / imageData.naturalHeight; |
|
|
|
|
|
|
288
|
|
|
cropLeft = (сropBoxData.left - canvasData.left) / ratio; |
|
|
|
|
|
|
289
|
|
|
cropTop = (сropBoxData.top - canvasData.top) / ratio; |
|
|
|
|
|
|
290
|
|
|
cropWidth = сropBoxData.width / ratio; |
|
|
|
|
|
|
291
|
|
|
cropHeight = сropBoxData.height / ratio; |
|
|
|
|
|
|
292
|
|
|
rotated = imageData.rotate; |
|
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
data = { |
|
|
|
|
|
|
295
|
|
|
id: currentCroppingImageId, |
|
296
|
|
|
width: cropWidth, |
|
297
|
|
|
height: cropHeight, |
|
298
|
|
|
top: cropTop, |
|
299
|
|
|
left: cropLeft, |
|
300
|
|
|
rotated: rotated, |
|
301
|
|
|
_fileFormToken: yii2FileFormToken |
|
|
|
|
|
|
302
|
|
|
}; |
|
303
|
|
|
|
|
304
|
|
|
removeFileOnCropCancel = false; |
|
305
|
|
|
|
|
306
|
|
|
$.ajax({ |
|
307
|
|
|
url: yii2CropRoute, |
|
|
|
|
|
|
308
|
|
|
'method': 'POST', |
|
309
|
|
|
data: data, |
|
310
|
|
|
success: function (response) { |
|
311
|
|
|
id = '#yii2-file-object-' + currentCroppingImageId; |
|
|
|
|
|
|
312
|
|
|
if ($(id).find('img').length) |
|
313
|
|
|
$(id).find('img').attr('src', response); |
|
|
|
|
|
|
314
|
|
|
else { |
|
315
|
|
|
$(id).css('background-image', 'none'); |
|
316
|
|
|
$(id).css('background-image', 'url(' + response + ')'); |
|
317
|
|
|
} |
|
318
|
|
|
stopCrop(); |
|
319
|
|
|
f12notification.info(FileSavedText, 1); |
|
|
|
|
|
|
320
|
|
|
}, |
|
321
|
|
|
error: function (response) { |
|
322
|
|
|
processError(response); |
|
323
|
|
|
} |
|
324
|
|
|
}) |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
function showRenameFileForm(id, event) { |
|
328
|
|
|
var blockId = '#yii2-file-object-' + id; |
|
329
|
|
|
var title = $(blockId).attr('title'); |
|
330
|
|
|
currentRenamingFileId = id; |
|
331
|
|
|
$('#yii2-file-title-editor').css('top', event.clientY).css('left', event.clientX - 70).fadeIn(100); |
|
332
|
|
|
$('#yii2-file-title-editor input').val(title).focus(); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
function showAltForm(id, event) { |
|
336
|
|
|
var blockId = '#yii2-file-object-' + id; |
|
337
|
|
|
var alt = $(blockId).data('alt'); |
|
338
|
|
|
console.log(alt); |
|
|
|
|
|
|
339
|
|
|
currentRenamingFileId = id; |
|
340
|
|
|
$('#yii2-file-alt-editor').css('top', event.clientY).css('left', event.clientX - 70).fadeIn(100); |
|
341
|
|
|
$('#yii2-file-alt-editor input').val(alt).focus(); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
function hideYii2FileTitleEditor() { |
|
345
|
|
|
$('#yii2-file-title-editor').fadeOut(100); |
|
346
|
|
|
currentRenamingFileId = null; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
function hideYii2FileAltEditor() { |
|
350
|
|
|
$('#yii2-file-alt-editor').fadeOut(100); |
|
351
|
|
|
currentRenamingFileId = null; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
function saveFileTitle() { |
|
355
|
|
|
$('#yii2-file-title-editor').fadeOut(100); |
|
356
|
|
|
val = $('#yii2-file-title-editor input').val(); |
|
|
|
|
|
|
357
|
|
|
blockId = '#yii2-file-object-' + currentRenamingFileId; |
|
|
|
|
|
|
358
|
|
|
$(blockId).attr('title', val); |
|
359
|
|
|
$(blockId).attr('data-title', val); |
|
360
|
|
|
|
|
361
|
|
|
$.ajax({ |
|
362
|
|
|
url: yii2RenameRoute, |
|
|
|
|
|
|
363
|
|
|
method: 'POST', |
|
364
|
|
|
data: {id: currentRenamingFileId, title: val, _fileFormToken: yii2FileFormToken}, |
|
|
|
|
|
|
365
|
|
|
success: function () { |
|
366
|
|
|
f12notification.info(FileRenamedText, 1); |
|
|
|
|
|
|
367
|
|
|
}, |
|
368
|
|
|
error: function (response) { |
|
369
|
|
|
processError(response); |
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
); |
|
373
|
|
|
currentRenamingFileId = null; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
function saveFileAlt() { |
|
377
|
|
|
$('#yii2-file-alt-editor').fadeOut(100); |
|
378
|
|
|
val = $('#yii2-file-alt-editor input').val(); |
|
|
|
|
|
|
379
|
|
|
blockId = '#yii2-file-object-' + currentRenamingFileId; |
|
|
|
|
|
|
380
|
|
|
$(blockId).data('alt', val) |
|
381
|
|
|
|
|
382
|
|
|
$.ajax({ |
|
383
|
|
|
url: yii2AltRoute, |
|
|
|
|
|
|
384
|
|
|
method: 'POST', |
|
385
|
|
|
data: {id: currentRenamingFileId, alt: val, _fileFormToken: yii2FileFormToken}, |
|
|
|
|
|
|
386
|
|
|
success: function () { |
|
387
|
|
|
f12notification.info(FileRenamedText, 1); |
|
|
|
|
|
|
388
|
|
|
}, |
|
389
|
|
|
error: function (response) { |
|
390
|
|
|
processError(response); |
|
391
|
|
|
} |
|
392
|
|
|
} |
|
393
|
|
|
); |
|
394
|
|
|
currentRenamingFileId = null; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
$(document).ready(function () { |
|
398
|
|
|
setInterval(function () { |
|
399
|
|
|
sortableFiles() |
|
400
|
|
|
}, 2000); |
|
401
|
|
|
|
|
402
|
|
|
sortableFiles(); |
|
403
|
|
|
|
|
404
|
|
|
initCropperLayout(); |
|
405
|
|
|
|
|
406
|
|
|
}); |
|
407
|
|
|
|
|
408
|
|
|
|
|
409
|
|
|
|